<template>
   <div class="app-container">
    <el-row :gutter="20">
      <splitpanes
        :horizontal="appStore.device === 'mobile'"
        class="default-theme"
      >
        <!--部门数据-->
        <pane size="18">
          <el-col>

      <el-table v-loading="loading" :data="collectionList" @row-click="handleSelectionChange">
         <el-table-column label="图片名称" align="center" prop="name" />
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.page"
         v-model:limit="queryParams.limit"
         @pagination="getList"
      />
         </el-col>
        </pane>
        <!--用户数据-->
        <pane size="80">     
          <el-col>
            <div id="mark-container" style="width: 60%; height: 80%;"></div>
          </el-col>
        </pane>
      </splitpanes>
   </el-row>
   </div>
</template>

<script setup name="Image">
import SimpleImageLabel from 'simple-image-label'
import { Splitpanes, Pane } from "splitpanes";
import { listImage, getImage, delImage, addImage, updateImage } from "@/api/image";
import { listDataset } from "@/api/dataset";
import { listCamera } from "@/api/camera";

import useAppStore from "@/store/modules/app";
const appStore = useAppStore();
const { proxy } = getCurrentInstance();
const sys_status = [{value:'0', label:'未标记'}, {value:'1', label:'已标记'}, {value:'2', label:'已审核'}];
const sys_type = [{value:'train', label:'训练集'}, {value:'test', label:'测试集'}, {value:'check', label:'验证集'}];
const collectionList = ref([]);
const dsList = ref([]);
const cameraList = ref([]);

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const baseURL = import.meta.env.VITE_APP_BASE_API
const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    name: undefined,
    code: undefined,
    status: undefined
  },
  rules: {
    name: [{ required: true, message: "采集任务名称不能为空", trigger: "blur" }],
    status: [{ required: true, message: "状态不能为空", trigger: "change" }],
    camera_id: [{ required: true, message: "摄像机不能为空", trigger: "change" }],
    ds_id: [{ required: true, message: "数据集不能为空", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);
const simpleImageLabel = ref(null);

/** 查询采集任务列表 */
function getList() {
  loading.value = true;
  listImage(queryParams.value).then(response => {
    collectionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    camera_id: undefined,
    ds_id: undefined,
    status: "0"
  };
  proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   console.log(selection)
   simpleImageLabel.value = new SimpleImageLabel({
        el: 'mark-container',
        imageUrl: baseURL + '/images/view/' + selection.id,
        labels: [],
        contextmenu: (e) => {
            emit('contextmenu', e)
        },
        labelClick: (label) => {
            emit('labelClick', label)
        },
        error: (e) => {
            emit('error', e)
        }
    });
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加图片";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const imageId = row.id || ids.value;
  getImage(imageId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改图片";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateImage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addImage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const imageId = row.id || ids.value
  proxy.$modal.confirm('是否确认删除图片编号为"' + imageId + '"的图片？').then(function() {
    return delImage(imageId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();

listDataset().then(response => {
   dsList.value = response.rows;
});
listCamera().then(response => {
   cameraList.value = response.rows;
});


</script>
